package redis

import (
	"common/helper"
	"github.com/shopspring/decimal"
	"strconv"
)

// 同hash获取多字段
func HMGetField(key string, fields []string) map[string]string {

	result := make(map[string]string)
	values, err := gRedis.HMGet(gTx, key, fields...).Result()
	if err != nil {
		helper.InfoLog("HMGetField err")
		return nil
	}
	for i, value := range values {

		if value == "" {
			result[fields[i]] = "" // 字段不存在时，将其值设置为空字符串
			continue
		}

		str, ok := value.(string)

		if !ok {
			helper.InfoLog("HMGetField change type err")
			result[fields[i]] = ""
			continue
		}

		result[fields[i]] = str

	}
	return result
}

func HMGetFieldInterface(key string, fields []string) map[string]interface{} {

	result := make(map[string]interface{})
	values, err := gRedis.HMGet(gTx, key, fields...).Result()
	if err != nil {
		helper.InfoLog("HMGetFieldInterface HMGetField err")
		return nil
	}
	for i, value := range values {

		if value == "" {
			result[fields[i]] = "" // 字段不存在时，将其值设置为空字符串
			continue
		}

		result[fields[i]] = value

	}
	return result
}

func HMGetFieldInt(key string, fields []string) map[string]int {

	result := make(map[string]int)

	values := HMGetField(key, fields)
	if nil == values {
		helper.InfoLog("HMGetFieldInt HMGetField err")
		return result
	}

	for key, value := range values {

		intValue, err := strconv.Atoi(value)
		if err != nil {
			helper.InfoLog("HMGetFieldInt change int type err")
			result[key] = 0
			continue
		}

		result[key] = intValue
	}

	return result
}

func HMGetFieldInt64(key string, fields []string) map[string]int64 {
	result := make(map[string]int64)

	values := HMGetField(key, fields)
	if nil == values {
		helper.InfoLog("HMGetFieldInt64 HMGetField err")
		return result
	}

	for key, value := range values {

		intValue, err := strconv.ParseInt(value, 10, 64)
		if err != nil {
			helper.InfoLog("HMGetFieldInt64 change int64 type err")
			result[key] = 0
			continue
		}

		result[key] = intValue
	}

	return result
}

func HMGetFieldInt8(key string, fields []string) map[string]int8 {

	result := make(map[string]int8)

	values := HMGetField(key, fields)
	if nil == values {
		helper.InfoLog("HMGetFieldInt8 HMGetField err")
		return result
	}

	for key, value := range values {

		intValue, err := strconv.Atoi(value)
		if err != nil {
			helper.InfoLog("HMGetFieldInt8 change int8 type err")
			result[key] = 0
			continue
		}

		result[key] = int8(intValue)
	}

	return result
}

func HMGetFieldFloat64(key string, fields []string) map[string]float64 {

	result := make(map[string]float64)

	values := HMGetField(key, fields)
	if nil == values {
		helper.InfoLog("HMGetFieldFloat64 HMGetField err")
		return result
	}

	for key, value := range values {

		intValue, err := strconv.ParseFloat(value, 64)
		if err != nil {
			helper.InfoLog("HMGetFieldFloat64 change float64 type err")
			result[key] = 0
			continue
		}

		result[key] = intValue
	}

	return result
}

func HMGetFieldDecimal(key string, fields []string) map[string]decimal.Decimal {

	result := make(map[string]decimal.Decimal)

	values := HMGetField(key, fields)
	if nil == values {
		helper.InfoLog("HMGetFieldDecimal HMGetField  err")
		return result
	}

	for key, value := range values {
		fVal, err := strconv.ParseFloat(value, 64)
		if err != nil {
			//log
			helper.InfoLog("HMGetFieldDecimal change type  err")

			continue
		}
		decVal := decimal.NewFromFloat(fVal).Div(decimal.NewFromInt(100))
		result[key] = decVal
	}

	return result
}

// 同hash写入多字段
func HMSetField(key string, fields map[string]string) {
	var args []interface{}
	for field, value := range fields {
		args = append(args, field)
		args = append(args, value)
	}

	_, err := gRedis.HMSet(gTx, key, args...).Result()
	if err != nil {
		helper.InfoLog("HMSetField  HMSet err")
	}
}

// demo
func RedisMultipleDemo() {
	HMSetField("redisMultipleDemo", map[string]string{
		"field1": "10",
		"field2": "20"})

	HMGetField("redisMultipleDemo", []string{"field1", "field2"})

	HMGetFieldInt("redisMultipleDemo", []string{"field1", "field2"})
	HMGetFieldFloat64("redisMultipleDemo", []string{"field1", "field2"})
}
